load("@aspect_rules_esbuild//esbuild:defs.bzl", "esbuild")
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("//src/components-examples:config.bzl", "ALL_EXAMPLES")
load("//tools:defaults.bzl", "http_server", "ng_project", "sass_binary")

package(default_visibility = ["//visibility:public"])

npm_link_all_packages()

ng_project(
    name = "dev-app",
    srcs = [
        "dev-app.ts",
        "main.ts",
        "polyfills.ts",
        "routes.ts",
    ],
    deps = [
        "//:node_modules/@angular/common",
        "//:node_modules/@angular/compiler",
        "//:node_modules/@angular/core",
        "//:node_modules/@angular/localize",
        "//:node_modules/@angular/platform-browser",
        "//:node_modules/@angular/router",
        "//src/cdk/bidi",
        "//src/cdk/overlay",
        "//src/dev-app/autocomplete",
        "//src/dev-app/badge",
        "//src/dev-app/baseline",
        "//src/dev-app/bottom-sheet",
        "//src/dev-app/button",
        "//src/dev-app/button-toggle",
        "//src/dev-app/card",
        "//src/dev-app/cdk-dialog",
        "//src/dev-app/cdk-experimental-accordion",
        "//src/dev-app/cdk-experimental-combobox",
        "//src/dev-app/cdk-experimental-listbox",
        "//src/dev-app/cdk-experimental-radio-group",
        "//src/dev-app/cdk-experimental-tabs",
        "//src/dev-app/cdk-experimental-tree",
        "//src/dev-app/cdk-listbox",
        "//src/dev-app/cdk-menu",
        "//src/dev-app/checkbox",
        "//src/dev-app/chips",
        "//src/dev-app/clipboard",
        "//src/dev-app/column-resize",
        "//src/dev-app/connected-overlay",
        "//src/dev-app/datepicker",
        "//src/dev-app/dev-app",
        "//src/dev-app/dialog",
        "//src/dev-app/drag-drop",
        "//src/dev-app/drawer",
        "//src/dev-app/example",
        "//src/dev-app/examples-page",
        "//src/dev-app/expansion",
        "//src/dev-app/focus-origin",
        "//src/dev-app/focus-trap",
        "//src/dev-app/google-map",
        "//src/dev-app/grid-list",
        "//src/dev-app/icon",
        "//src/dev-app/input",
        "//src/dev-app/input-modality",
        "//src/dev-app/layout",
        "//src/dev-app/list",
        "//src/dev-app/live-announcer",
        "//src/dev-app/menu",
        "//src/dev-app/menubar",
        "//src/dev-app/paginator",
        "//src/dev-app/performance",
        "//src/dev-app/platform",
        "//src/dev-app/popover-edit",
        "//src/dev-app/portal",
        "//src/dev-app/progress-bar",
        "//src/dev-app/progress-spinner",
        "//src/dev-app/radio",
        "//src/dev-app/ripple",
        "//src/dev-app/screen-type",
        "//src/dev-app/select",
        "//src/dev-app/selection",
        "//src/dev-app/sidenav",
        "//src/dev-app/slide-toggle",
        "//src/dev-app/slider",
        "//src/dev-app/snack-bar",
        "//src/dev-app/stepper",
        "//src/dev-app/table",
        "//src/dev-app/table-scroll-container",
        "//src/dev-app/tabs",
        "//src/dev-app/theme",
        "//src/dev-app/timepicker",
        "//src/dev-app/toolbar",
        "//src/dev-app/tooltip",
        "//src/dev-app/tree",
        "//src/dev-app/typography",
        "//src/dev-app/virtual-scroll",
        "//src/dev-app/youtube-player",
        "//src/material/core",
    ],
)

esbuild(
    name = "bundles",
    srcs = [
        "//src:build-tsconfig",
    ],
    bundle = True,
    config = "//src:esbuild-linked-config",
    entry_points = [
        ":main.js",
        ":polyfills.js",
    ] + ["%s:index.js" % e for e in ALL_EXAMPLES],
    platform = "browser",
    sourcemap = "linked",
    sources_content = True,
    splitting = True,
    # We cannot use `ES2017` or higher as that would result in `async/await` not being downleveled.
    # ZoneJS needs to be able to intercept these as otherwise change detection would not work properly.
    target = "es2016",
    deps = ALL_EXAMPLES + [
        ":dev-app",
    ],
)

sass_binary(
    name = "theme",
    src = "theme.scss",
    deps = [
        "//src/material:sass_lib",
        "//src/material-experimental:sass_lib",
    ],
)

sass_binary(
    name = "theme_m3",
    src = "theme-m3.scss",
    deps = [
        "//src/material:sass_lib",
        "//src/material-experimental:sass_lib",
    ],
)

# Variables that are going to be inlined into the dev app index.html.
filegroup(
    name = "variables",
    # Note that we need the * in the pattern, because there's a lint rule which
    # doesn't allow single files in a `glob`. We have to use a glob, because the file is optional.
    srcs = glob(["*variables.json"]),
)

# File group for all static files which are needed to serve the dev-app. These files are
# used in the devserver as runfiles and will be copied into the static web package that can
# be deployed on static hosting services (like firebase).
filegroup(
    name = "dev_app_static_files",
    srcs = [
        "favicon.ico",
        "index.html",
        ":theme",
        ":theme_m3",
        ":variables",
        "//:node_modules/moment",
        "//:node_modules/zone.js",
        "//src/dev-app/icon:icon_demo_assets",
    ],
)

http_server(
    name = "devserver",
    srcs = [":dev_app_static_files"],
    additional_root_paths = [
        "angular_material/",
    ],
    enable_dev_ui = True,
    # List of environment variables that will be made available as `window.<NAME>` in the
    # served `index.html` throuhg an injected script. Useful for allowing developers to
    # configure API keys without requiring secrets to be committed.
    environment_variables = [
        "GOOGLE_MAPS_KEY",
    ],
    tags = ["manual"],
    deps = [
        ":bundles",
    ],
)

# Target that builds a static web package of the dev-app. The web package can be
# deployed on static hosting services (such as firebase).
npm_package(
    name = "web_package",
    srcs = [
        ":bundles",
        ":dev_app_static_files",
    ],
    out = "web_package",
    exclude_srcs_patterns = [
        "node_modules/.aspect_rules_js/**",
    ],
    include_srcs_packages = ["**"],
    root_paths = [
        ".",
        "",
    ],
    tags = ["manual"],
)
